const {
	editUrl,
	listUrl,
	listUserUrl,
	otherUserUrl,
} = require('@utils/util-apis')['teamUrl']
const {
	reqPost,
} = require('@utils/request')
const {
	$U_UserInfo,
} = require('@utils/storage')

module.exports = function initTeamPage() {
	let {
		teamComp,
	} = $['components-jq']
	teamComp.dailogComp = {
		formDialog: $("#team-form-dialog"),
	}
	teamComp.dataGrid = $("#main-tabs-team")
	teamComp.teamForm = $("#team-form")
	teamComp.teamName = $("#team-form-name")
	teamComp.teamIntro = $("#team-form-introduction")
	teamComp.userList = $("#team-user-has")
	teamComp.userOther = $("#team-user-other")
	initDailog(teamComp)
	initDataGrid(teamComp)
	initTeamUserList(teamComp)
}

let _editFlag = 'add'
let _teamId = ''
let _modifyUser = false

function initDailog(teamComp) {
	let {
		dataGrid,
		teamForm,
		dailogComp,
		formBtnSave,
		formBtnCancel,
		userAccount,
		userEmail,
		userPassword,
		userRealName,
		userList,
	} = teamComp
	let {
		formDialog,
	} = dailogComp
	formDialog.dialog({
		iconCls: 'icon-save',
		resizable: true,
		modal: true,
		closed: true,
		top: 80,
		buttons: [{
			text: '保存',
			iconCls: 'icon-save',
			handler() {
				$.messager.progress();
				teamForm.form('submit', {
					url: editUrl,
					onSubmit(param) {
						let isValid = teamForm.form('validate');
						if (!isValid) {
							$.messager.progress('close');
							return false
						}
						let userInfo = $U_UserInfo()
						param.user_id = userInfo.id
						param.flag = _editFlag
						param.teamId = _teamId
						param.modify = _modifyUser
						let idList = []
						let userRows = userList.datagrid('getRows')
						for (let i = 0; i < userRows.length; i++) {
							let user = userRows[i]
							idList.push(user.id)
						}
						param.userIdList = JSON.stringify(idList)
						return true;
					},
					success() {
						$.messager.progress('close');
						dataGrid.datagrid('reload');
						formDialog.dialog('close');
						teamForm.form('clear')
					}
				})
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler() {
				teamForm.form('clear')
				formDialog.dialog('close')
			}
		}]
	})
}

function initDataGrid(teamComp) {
	let userInfo = $U_UserInfo()
	let {
		dataGrid,
		teamForm,
		dailogComp,
		userList,
		userOther,
	} = teamComp
	let {
		formDialog,
	} = dailogComp
	dataGrid.datagrid({
		fit: true,
		url: listUrl,
		rownumbers: true,
		fitColumns: true,
		singleSelect: true,
		queryParams: {
			user_id: userInfo.id,
			user_role: userInfo.user_role,
		},
		toolbar: [{
			iconCls: 'icon-add',
			text: '新建团队',
			handler() {
				_editFlag = 'add'
				teamForm.form('clear')
				userList.datagrid('loadData', [userInfo])
				reqPost(otherUserUrl, {
					userId: userInfo.id,
				}).then(result => {
					userOther.datagrid('loadData', result)
				});
				formDialog.dialog('open');
			}
		}, '-', {
			iconCls: 'icon-edit',
			text: '修改',
			handler() {
				_editFlag = 'update'
				let row = dataGrid.datagrid('getSelected')
				if (!row)
					return false
				teamForm.form('load', row)
				let teamId = _teamId = row.id;
				// 已有成员
				reqPost(listUserUrl, {
					teamId,
				}).then(result => {
					userList.datagrid('loadData', result)
				});
				// 其他成员
				reqPost(otherUserUrl, {
					userId: userInfo.id,
					teamId,
				}).then(result => {
					userOther.datagrid('loadData', result)
				});
				formDialog.dialog('open');
			}
		}],
		columns: [
			[{
				field: 'id',
				title: 'ID',
				width: 5
			}, {
				field: 'name',
				title: '团队名称',
				width: 15
			}, {
				field: 'create_user',
				title: '创建人',
				width: 15,
			}, {
				field: 'create_date',
				title: '创建时间',
				width: 20,
			}, {
				field: 'introduction',
				title: '团队描述',
				width: 60,
			}]
		]
	})
}

function initTeamUserList(teamComp) {
	let userInfo = $U_UserInfo()
	let {
		dataGrid,
		userList,
		userOther,
	} = teamComp
	userList.datagrid({
		fit: true,
		rownumbers: true,
		fitColumns: true,
		// singleSelect: true,
		toolbar: [{
			iconCls: 'icon-remove',
			text: '删除成员',
			handler() {
				let row = dataGrid.datagrid('getSelected')
				if (row.create_user != userInfo.realname) {
					$.messager.alert({
						title: '警告',
						msg: '没有权限操作！',
						icon: 'warning'
					})
					return false
				}
				_modifyUser = true
				let checkedArr = userList.datagrid('getSelections')
				for (let i = 0; i < checkedArr.length; i++) {
					let item = checkedArr[i]
					let itemIdx = userList.datagrid('getRowIndex', item)
					if (itemIdx == 0) {
						continue;
					}
					userList.datagrid('deleteRow', itemIdx)
					userOther.datagrid('appendRow', item)
				}
			}
		}],
		columns: [
			[{
				field: 'id',
				title: 'ID',
				width: 20
			}, {
				field: 'realname',
				title: '真实姓名',
				width: 60,
			}, {
				field: 'email',
				title: '邮箱',
				width: 80,
			}]
		],
	});

	userOther.datagrid({
		fit: true,
		rownumbers: true,
		fitColumns: true,
		toolbar: [{
			iconCls: 'icon-add',
			text: '添加成员',
			handler() {
				let row = dataGrid.datagrid('getSelected')
				if (row && row.create_user != userInfo.realname) {
					$.messager.alert({
						title: '警告',
						msg: '没有权限操作！',
						icon: 'warning'
					})
					return false
				}
				_modifyUser = true
				let checkedArr = userOther.datagrid('getSelections')
				for (let i = 0; i < checkedArr.length; i++) {
					let item = checkedArr[i]
					let itemIdx = userOther.datagrid('getRowIndex', item)
					userOther.datagrid('deleteRow', itemIdx)
					userList.datagrid('appendRow', item)
				}
			}
		}],
		columns: [
			[{
				field: 'id',
				title: 'ID',
				width: 20
			}, {
				field: 'realname',
				title: '真实姓名',
				width: 60,
			}, {
				field: 'email',
				title: '邮箱',
				width: 80,
			}]
		],
	})
}